import {
  indexApi
} from '../../../api/index';
import {
  request
} from '../../../utils/request';

Page({
  data: {
    defaultData: {
      title: "服务确认", // 导航栏标题
      imageSrc: "/images/back.png",
      isBackToPreviousPage: true
    },
    radio: null,
    checked: false,
    orderDetail:{
      address:"", //地址
    },
    address:"", //地址
    workerList:[]
  },
  onLoad(options) {
    console.log("/packageOrder/pages/orderSure/index");
    if (options.order) {
      const order = JSON.parse(decodeURIComponent(options.order));
      console.log("order",order);
      const selectedAddressObj=order.address
      const selectedAddress = `${selectedAddressObj.province} ${selectedAddressObj.city} ${selectedAddressObj.area} ${selectedAddressObj.detail}`;
      console.log("selectedAddress",selectedAddress);
      this.setData({
        workerList: order.reviceOrders,
        address:selectedAddress
      });
      console.log("Received order:", this.data.workerList);
    }
  },

  onChange(event) {
    this.setData({
      radio: event.detail,
    });
    console.log("radio",this.data.radio);
  },

  onProtocol(event) {
    this.setData({
      checked: event.detail,
    });
  },

  backOrder() {
    wx.navigateBack({
      delta: 1
    });
  },

  async confireService() {  
    const { radio, workerList } = this.data;  
    let targetUser = workerList.find(user => user.reviceOrderId == radio);  
    const {orderId,reviceOrderId} = targetUser    
    console.log("orderId,orderId",orderId);
    const reqTemplate={
      orderId:orderId,
      reviceOrderId:reviceOrderId
    }
    try {
      const res = await request({
        url: indexApi.confrimOrderApi(),
        method:"POST",
        data:reqTemplate         
      });
      console.log(res);
      wx.switchTab({
        url: '/pages/order/index',
      })
    } catch (error) {
      console.error("Error:", error);
    }
      
  }
})